<?php

namespace MyBlog\Http\Controllers\desktop;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use MyBlog\Http\Requests;
use MyBlog\Http\Controllers\Controller;
use MyBlog\Models\Article;
use MyBlog\Models\Category;
use MyBlog\Models\Comment;
use Illuminate\Support\Facades\Input;

class ArticleController extends Controller
{
    public function index()
    {
    	$articles = Article::where('disabled', false)
    			->orderBy('id', 'DESC');
    	if(Input::get('categoryName')) {
	    	$categoryName = Input::get('categoryName');
	    	$category = Category::where('name', $categoryName)->first();
	    	if(is_null($category)) {
	    		return redirect('/404');
	    	}
	    	$articles = $articles->where('category_id', $category->id);
    	}
        $articles = $articles->paginate(10);
        return View::make('desktop.articles', compact('articles'));
    }

    public function show($id)
    {
        $article = Article::find($id);
        $article->read = $article->read + 1;
        $article->save();
    	$categories = Category::where('disabled', false)
    			->where('type', Category::TYPE_ARTICLES)
    			->orderBy('order', 'Asc')->get();
        $newComments = Comment::orderBy('id', 'DESC')->take('3')->get();
        $hotArticles = Article::orderBy('read', 'Desc')->take('3')->get();
        return View::make('desktop.article', compact('article', 'categories', 'newComments', 'hotArticles'));
    }
}
